package lgkj.meicqt20.ui;


import android.app.Activity;
import android.app.Presentation;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.ImageView;

import java.util.List;

import lgkj.meicqt20.MainActivity;
import lgkj.meicqt20.R;
import lgkj.meicqt20.bean.AdverBean;
import lgkj.meicqt20.utils.CommUtils;
import lgkj.meicqt20.utils.DbController;
import lgkj.meicqt20.utils.TimerUtils;
import lgkj.meicqt20.view.CustomTextView;

/**
 * Created by Administrator on 2019/2/15 0015.
 */

public class DifferentDislayActivity extends Presentation {
    private static final String TAG = "DifferentDislayActivity";
    ImageView bannerLeft;
    private CustomTextView tvGuanggao;
    private Context context;
    private int position;
    private List<AdverBean> adverBeans;
    private DbController dbController;
    private TimerUtils timerUtils;
    private boolean sile = true;
    private String adTexts = "";

    public DifferentDislayActivity(Context outerContext, Display display) {
        super(outerContext, display);
        this.context = outerContext;
        position = 1;
        dbController = new DbController(outerContext);
        adverBeans = dbController.searchAll();
        position = 0;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_different_display);
        bannerLeft = findViewById(R.id.banner_left);
        tvGuanggao = findViewById(R.id.tv_guanggao);
        timerUtils = new TimerUtils(callBack, 15000);
        adTexts = MainActivity.adTexts;
        showImage();
        timerUtils.start();
        guanggao();
    }

    private void startText() {
        Log.i(TAG, "handleMessage: " + (!tvGuanggao.getIsMarguee() && adTexts != null && adTexts.length() > 0));
        if (!tvGuanggao.getIsMarguee() && adTexts != null && adTexts.length() > 0) {
            tvGuanggao.setVisibility(View.VISIBLE);
            tvGuanggao.setTxt(adTexts);
            tvGuanggao.startScroll();
        }
    }

    private TimerUtils.UpdateUiCallBack callBack = new TimerUtils.UpdateUiCallBack() {
        @Override
        public void updateUI(String text) {
            position++;
            showImage();
            startText();
        }
    };

    @Override
    protected void onStop() {
        super.onStop();
        timerUtils.stop();
        timerUtils.destroy();
        Log.i(TAG, "onStop: ");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "start: ");
    }


    private void guanggao() {
        new Thread() {
            public void run() {
                Activity activity = (Activity) context;
                tvGuanggao.init(activity.getWindowManager());
                tvGuanggao.startScroll();
                if (!"".equals(adTexts)) {
                    tvGuanggao.setTxt(adTexts);
                }
                tvGuanggao.setOnMargueeListener(new CustomTextView.OnMargueeListener() {
                    @Override
                    public void onRollOver() {
                        Log.i(TAG, "onRollOver: 滚动完毕");
//                        Toast.makeText(FirstActivity.this, "滚动完毕" + tv_guanggao.getIsMarguee(), Toast.LENGTH_SHORT).show();
                    }
                });
                Log.i(TAG, "mainMethod: " + tvGuanggao.getIsMarguee());
            }
        }.start();  //开启一个线程
    }

    private void showImage() {
        try {
            String url = adverBeans.get(position).getAd_cover();
//            Log.i(TAG, "showImage: " + position + ",url:" + url);
            CommUtils.LoadImage(context, bannerLeft, url, 0);
        } catch (Exception e) {
            Log.e(TAG, "showImage: " + e.getMessage());
        }
    }


}
